package ru.volgogradetzzz.text 
{
	//--------------------------------------
	// Imports
	//--------------------------------------
	
	import flash.text.Font;
	
	//--------------------------------------
	// Class description
	//--------------------------------------
	
	/**
	 * VTextFormat contains option for text formatting.
	 * 
	 * @author Volgogradetzzz
	 */
	public class VTextFormat 
	{
		//--------------------------------------
		// Private fields
		//--------------------------------------
		
		private var _horizontalAlign:String;
		private var _bold:Boolean;
		private var _color:uint;
		private var _embedFont:Boolean;
		private var _font:String;
		private var _italic:Boolean;
		private var _size:int;
		private var _verticalAlign:String;
		private var _lineSpace:Number;
		
		/**
		 * Changing 'font' property takes too much time.
		 * So it would be better to store value of current font and change 'font' property only if that value changed.
		 */
		private var _currentFont:String;
		
		//--------------------------------------
		// Constructor
		//--------------------------------------
		
		/**
		 * Creates new instance of VTextFormat.
		 * 
		 * @param	font 			Font name.
		 * @param	size 			Font size.
		 * @param	color 			Font color.
		 * @param	bold 			Font weight.
		 * @param	italic 			Font style.
		 * @param	embedFont 		Embed font usage.
		 * @param	horizontalAlign	Text horizontal align,
		 * @param	verticalAlign	Text vertical align.
		 * @param	lineSpace 		Space between lines.
		 */
		public function VTextFormat(font:String = "Arial", size:int = 20, color:uint = 0x000000, bold:Boolean = false, italic:Boolean = false, embedFont:Boolean = false, horizontalAlign:String = 'left', verticalAlign:String = 'top', lineSpace:Number = 0) 
		{
			_font = font;
			_size = size;
			_color = color;
			_bold = bold;
			_italic = italic;
			_embedFont = embedFont;
			_horizontalAlign = horizontalAlign;
			_verticalAlign = verticalAlign;
			_lineSpace = lineSpace;
			
			_currentFont = _font;
		}
		
		//--------------------------------------
		// Public methods
		//--------------------------------------
		
		/**
		 * Returns copy of text format.
		 * @return New instance of VTextFormat.
		 */
		public function clone():VTextFormat
		{
			return new VTextFormat(_font, _size, _color, _bold, _italic, _embedFont, _horizontalAlign, _verticalAlign, _lineSpace);
		}
		
		//--------------------------------------
		// Public properties
		//--------------------------------------
		
		/**
		 * Gets or sets text horizontal align in text field. Possible values are 'left', 'center' or 'right'.
		 */
		public function set horizontalAlign(value:String):void 
		{
			if (value != VTextAlign.LEFT && value != VTextAlign.RIGHT && value != VTextAlign.CENTER)
			{
				throw(new Error('Text align should contain "left", "center" or "right" values.'));
			}
			
			_horizontalAlign = value;
		}
		/**
		 * @private
		 */
		public function get horizontalAlign():String { return _horizontalAlign; }
		
		/**
		 * Gets or sets text horizontal align in text field. Possible values are 'top', 'middle' or 'bottom'.
		 */
		public function set verticalAlign(value:String):void 
		{
			if (value != VTextAlign.TOP && value != VTextAlign.BOTTOM && value != VTextAlign.MIDDLE)
			{
				throw(new Error('Text vertical align should contain "top", "middle" or "bottom" values.'));
			}
			
			_verticalAlign = value;
		}
		/**
		 * @private
		 */
		public function get verticalAlign():String { return _verticalAlign; }
		
		/**
		 * Gets or sets text weight.
		 */
		public function set bold(value:Boolean):void 
		{
			_bold = value;
		}
		/**
		 * @private
		 */
		public function get bold():Boolean { return _bold; }
		
		/**
		 * Gets or sets text style.
		 */
		public function set italic(value:Boolean):void 
		{
			_italic = value;
		}
		/**
		 * @private
		 */
		public function get italic():Boolean { return _italic; }
		
		/**
		 * Gets or sets text color.
		 */
		public function set color(value:uint):void 
		{
			_color = value;
		}
		/**
		 * @private
		 */
		public function get color():uint { return _color; }
		
		/**
		 * Gets or sets text size. Size must be greater than or equal to 0.
		 */
		public function set size(value:int):void 
		{
			if (value < 0)
			{
				throw(new Error('Font size should be greater than 0.'));
			}
			
			_size = value;
		}
		/**
		 * @private
		 */
		public function get size():int { return _size; }
		
		/**
		 * Gets or sets space between lines.
		 */
		public function set lineSpace(value:Number):void 
		{
			_lineSpace = value;
		}
		/**
		 * @private
		 */
		public function get lineSpace():Number { return _lineSpace; }
		
		/**
		 * Gets or sets text font. If no font found, default font used.
		 * 
		 * @default 'Arial'
		 */
		public function set font(value:String):void 
		{
			if (_currentFont == value) return;
			
			var fontArr:Array = Font.enumerateFonts(true);
			for each(var f:Font in fontArr)
			{
				if (f.fontName == value)
				{
					_font = value;
					_currentFont = _font;
					
					embedFont = _embedFont;
					
					return;
				}
			}
			
			_font = '_serif';
			_currentFont = _font;
		}
		/**
		 * @private
		 */
		public function get font():String { return _font; }
		
		/**
		 * Specifies to use embed font or not. If use embed font specifies but no such font found then
		 * default 'Arial' font used and 'embedFont' sets to 'false';
		 */
		public function set embedFont(value:Boolean):void 
		{
			if (value)
			{
				var fontArr:Array = Font.enumerateFonts(false);
				for each(var f:Font in fontArr)
				{
					if (f.fontName == _font)
					{
						_embedFont = value;
						return;
					}
				}
				
				trace("Couldn't find embedded font. Font set to default and embedFont set to false.");
				_font = '';
				_embedFont = false;
			}
			else
			{
				_embedFont = false;
			}
		}
		/**
		 * @private
		 */
		public function get embedFont():Boolean { return _embedFont; }
	}
}